package com.wsk.controller;

import com.wsk.empty.Empty;
import com.wsk.encrypt.Encrypt;
import com.wsk.pojo.PublishCritic;
import com.wsk.pojo.UserInformation;
import com.wsk.service.PublishCriticService;
import com.wsk.service.UserInformationService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

/**
 * Created by wsk1103 on 2017/5/1.
 */
@Controller
public class CriticController {

    @Resource(name = "publishCriticService")
    private PublishCriticService publishCriticService;
    @Resource
    private UserInformationService userInformationService;

    @RequestMapping(value = "publishCritic", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map publishCritic(HttpServletRequest request,
                             @RequestParam(value = "file_upload", required = false) MultipartFile file_upload,
                             @RequestParam String movie_name, @RequestParam String movie_content, @RequestParam int isPrivate) {
        String phone_session = (String) request.getSession().getAttribute("phone_session");
        String password_session = (String) request.getSession().getAttribute("password_session");
        String path = "";
        Map<String, String> map = new HashMap();
        if (movie_content.length() < 1 || movie_name.length() < 1) {
            map.put("result", "0");
            return map;
        }
        if (Empty.isNullOrEmpty(phone_session) || Empty.isNullOrEmpty(password_session)) {
            map.put("result", "0");
            return map;
        }
        if (Empty.isNullOrEmpty(file_upload)) {
            map.put("result", "0");
        } else {
            path = request.getSession().getServletContext().getRealPath("userPicture");
//            path += "/image/userPicture/";
            String fileName = Encrypt.getRandom() + new Date().getTime() + ".jpg";
//        System.out.println(path+"\\"+fileName);
            File image = new File(path, fileName);
            if (!image.exists())
                image.mkdirs();
            try {
                file_upload.transferTo(image);
            } catch (Exception e) {
                map.put("result", "0");
                e.printStackTrace();
                return map;
            }
            path = "localhost:8080" + request.getContextPath() + "/" + phone_session + "/" + fileName;
        }
        PublishCritic publishCritic = new PublishCritic();
        publishCritic.setCritic(movie_content);
        publishCritic.setGood(0);
        publishCritic.setIsprivate(isPrivate);
        publishCritic.setPicture(path);
        publishCritic.setTime(new Date());
        publishCritic.setTitle(movie_name);
        publishCritic.setUid((Integer) request.getSession().getAttribute("uid"));
        publishCritic.setAllow((short) 1);
        publishCritic.setModified(new Date());
        int id_result = publishCriticService.insertObject(publishCritic);
        if (id_result != 1) {
            map.put("result", "0");
            return map;
        }
        map.put("result", "1");
        return map;
    }

    @RequestMapping(value = "test")
    public String  test(Model model) {
        try {
            List<Integer> list = new ArrayList<>();
            list.add(1);
            list.add(2);
//        list.add(3);
//        list.add(4);
            List<UserInformation> userInformations;
            List<PublishCritic> publishCritics;
            userInformations = userInformationService.getAllForeach(list);
            List<Integer> ids = new ArrayList<>();
            for (UserInformation userInformation : userInformations) {
                ids.add(userInformation.getId());
            }
            publishCritics = publishCriticService.getAllForeach(ids);
            for (PublishCritic publishCritic : publishCritics) {
                System.out.println(publishCritic.getCritic());
            }
            model.addAttribute("users", publishCritics);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "all";
    }
}
